/*********************************************************************
 *                                                                   *
 *  Js Css Minifier ( JsCssMin )                                     *
 *  Author: Igor Kanshyn                                             *
 *  Copyright (c) 2009, Igor Kanshyn. All rights reserved.           *
 *                                                                   *
 *  JsCssMin is released under the Apache Software License.          *
 *  See LICENSE.txt for more details.                                *
 *                                                                   *
 *********************************************************************/

package org.grenader.jscssmin.model;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import org.grenader.jscssmin.*;
import org.grenader.jscssmin.files.FileUtils;
import org.grenader.jscssmin.files.ResourceOriginalFileIterator;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

/**
 * <p>Product: JsCssMin ( JavaScript and CSS Minifier )<br>
 * Author: Igor Kanshyn (grenader).
 * </p>
 * Date: Apr 26, 2009
 */
public class ResourceBlockTestCase extends FileTestCaseBase {

    @Test
    public void testGetBlockContent_nonStandartExtensions() {
        ResourceBlock block = new ResourceBlock();
        block.add(ResourceUtils.createResource("<link rel=\"stylesheet\" href=\"my1.style\" type=\"text/css\" />"));
        block.add(ResourceUtils.createResource("<script src=\"js2.javaS\" type=\"text/javascript\"></script>"));

        block.processBlock(new EmptyJsCssProcessor());
        String resText = block.getBlockContent("2");
        assertEquals("<link type=\"text/css\" rel=\"stylesheet\" href=\"my1.style_v2.css\"/>\n" +
                "<script type=\"text/javascript\" src=\"js2.javaS_v2.js\"></script>\n", resText);
    }

    @Test
    public void testGetBlockContent_JavaScript() {
        ResourceBlock block = new ResourceBlock();
        block.add(ResourceUtils.createResource("<script src=\"js1.js\" type=\"text/javascript\"></script>"));
        block.add(ResourceUtils.createResource("<script src=\"js2.js\" type=\"text/javascript\"></script>"));

        block.processBlock(new EmptyJsCssProcessor());
        String resText = block.getBlockContent("4");
        assertEquals("<script type=\"text/javascript\" src=\"js1_js2_v4.js\"></script>\n", resText);
    }

    @Test
    public void testGenerateFullContent_mixedContent_twoGroups() {
        ResourceBlock block = new ResourceBlock();
        block.add(ResourceUtils.createResource("<link rel=\"stylesheet\" href=\"my1.css\" type=\"text/css\" />"));
        block.add(ResourceUtils.createResource("<script src=\"js2.js\" type=\"text/javascript\"></script>"));
        block.add(ResourceUtils.createResource("<link rel=\"stylesheet\" href=\"my2.css\" type=\"text/css\" />"));
        block.add(ResourceUtils.createResource("<script src=\"js1.js\" type=\"text/javascript\"></script>"));

        block.processBlock(new EmptyJsCssProcessor());
        String resText = block.getBlockContent("3");
        assertEquals("<link type=\"text/css\" rel=\"stylesheet\" href=\"my1_my2_v3.css\"/>\n" +
                "<script type=\"text/javascript\" src=\"js2_js1_v3.js\"></script>\n", resText);
    }


    @Test
    public void testProcessFiles_CSS() throws IOException {

        // config and processor definition
        JsCssMinConfig config = new JsCssMinConfig();
        config.setCurrentDirAbsolutePath(tmpOriginalDir.getAbsolutePath());
        config.setDestDirAbsolutePath(tmpDestinatonDir.getAbsolutePath());
        config.setVersion("3");
        JsCssMinProcessor cssMinProcessor = new JsCssMinProcessor(config);

        // prepare model data
        Resource resource1 = ResourceUtils.createResource("<link rel=\"stylesheet\" href=\"my1.css\" type=\"text/css\" />");
        Resource resource2 = ResourceUtils.createResource("<link rel=\"stylesheet\" href=\"my2.css\" type=\"text/css\" />");
        resource1.setProcessed(cssMinProcessor.processResource(resource1));
        resource2.setProcessed(cssMinProcessor.processResource(resource2));

        ResourceBlock block = new ResourceBlock();
        block.add(resource1);
        block.add(resource2);

        block.processBlock(cssMinProcessor);

        // preliminary checks       
        ResourceGroup cssGroup = block.getTypesMap().get(ResourceType.CSS);
        assertEquals(2, cssGroup.getResources().size());

        // processing check
        for (Resource resource : cssGroup.getResources()) {
            assertTrue(resource.isProcessed());
        }

        File resultFile = new File(tmpDestinatonDir, cssGroup.getGroupFileName(config.getVersion()));
        System.out.println("resultFile.getAbsolutePath() = " + resultFile.getAbsolutePath());
        assertTrue(resultFile.exists());
    }


    @Test
    public void testProcessFiles_Javascript() throws IOException {
        JsCssMinConfig config = new JsCssMinConfig();
        config.setCurrentDirAbsolutePath(tmpOriginalDir.getAbsolutePath());
        config.setDestDirAbsolutePath(tmpDestinatonDir.getAbsolutePath());
        config.setVersion("4");
        JsCssMinProcessor processor = new JsCssMinProcessor(config);

        // prepare model data
        Resource resource1 = ResourceUtils.createResource("<script src=\"my1.js\" type=\"text/javascript\"></script>");
        Resource resource2 = ResourceUtils.createResource("<script src=\"my2.js\" type=\"text/javascript\"></script>");
        resource1.setProcessed(processor.processResource(resource1));
        resource2.setProcessed(processor.processResource(resource2));

        ResourceBlock block = new ResourceBlock();
        block.add(resource1);
        block.add(resource2);

        block.processBlock(processor);

        // preliminary checks
        ResourceGroup jsGroup = block.getTypesMap().get(ResourceType.JavaScript);
        assertEquals(2, jsGroup.getResources().size());

        // processing check
        for (Resource resource : jsGroup.getResources()) {
            assertTrue(resource.isProcessed());
        }

        File resultFile = new File(tmpDestinatonDir, jsGroup.getGroupFileName(config.getVersion()));
        System.out.println("resultFile.getAbsolutePath() = " + resultFile.getAbsolutePath());
        assertTrue(resultFile.exists());
    }


    @Test
    public void testProcessFiles_mix() throws IOException {
        JsCssMinConfig config = new JsCssMinConfig();
        config.setCurrentDirAbsolutePath(tmpOriginalDir.getAbsolutePath());
        config.setDestDirAbsolutePath(tmpDestinatonDir.getAbsolutePath());
        config.setVersion("4");
        JsCssMinProcessor processor = new JsCssMinProcessor(config);

        // prepare model data
        ResourceBlock block = new ResourceBlock();
        block.add(ResourceUtils.createResource("<script src=\"my1.js\" type=\"text/javascript\"></script>"));
        block.add(ResourceUtils.createResource("<link rel=\"stylesheet\" href=\"my1.css\" type=\"text/css\" />"));
        block.add(ResourceUtils.createResource("<script src=\"my2.js\" type=\"text/javascript\"></script>"));
        block.add(ResourceUtils.createResource("<link rel=\"stylesheet\" href=\"my2.css\" type=\"text/css\" />"));

        for (Resource resource : block.getResourceList()) {
            resource.setProcessed(processor.processResource(resource));
        }

        block.processBlock(processor);

        // preliminary checks
        ResourceGroup cssGroup = block.getTypesMap().get(ResourceType.CSS);
        assertEquals(2, cssGroup.getResources().size());
        ResourceGroup jsGroup = block.getTypesMap().get(ResourceType.JavaScript);
        assertEquals(2, jsGroup.getResources().size());

        File cssResultFile = new File(tmpDestinatonDir, cssGroup.getGroupFileName(config.getVersion()));
        System.out.println("cssResultFile.getAbsolutePath() = " + cssResultFile.getAbsolutePath());
        assertTrue(cssResultFile.exists());

        File jsResultFile = new File(tmpDestinatonDir, jsGroup.getGroupFileName(config.getVersion()));
        System.out.println("jsResultFile.getAbsolutePath() = " + jsResultFile.getAbsolutePath());
        assertTrue(jsResultFile.exists());
    }


    @Test
    public void testConcatenateFiles() throws IOException {

        ResourceBlock block = new ResourceBlock();
        block.add(ResourceUtils.createResource("<link rel=\"stylesheet\" href=\"my1.css\" type=\"text/css\" />"));
        block.add(ResourceUtils.createResource("<link rel=\"stylesheet\" href=\"my2.css\" type=\"text/css\" />"));
        block.createGroups(new JsCssMinConfig());

        ResourceGroup group = block.getTypesMap().get(ResourceType.CSS);
        ResourceOriginalFileIterator resourceOriginalFileIterator = new ResourceOriginalFileIterator(tmpOriginalDir.getAbsolutePath(), group);

        // check enumeration
        int counter = 0;
        assertTrue(resourceOriginalFileIterator.hasNext());
        while (resourceOriginalFileIterator.hasNext()) {
            File inputStream = resourceOriginalFileIterator.next();
            counter++;
        }
        assertEquals(2, counter);

        ResourceOriginalFileIterator resourceOriginalFileIterator2 = new ResourceOriginalFileIterator(tmpOriginalDir.getAbsolutePath(), group);
        FileUtils.copyFilesIntoOne(new File(tmpDestinatonDir, "res-file.jsCss"), resourceOriginalFileIterator2);

        assertTrue(new File(tmpDestinatonDir, "res-file.jsCss").length() > 10);
    }

}
